package com.exam.serviceimpl;

import com.exam.entity.QuestionRule;
import com.exam.mapper.QuestionRuleRepository;
import com.exam.service.QuestionRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class QuestionRuleServiceImpl implements QuestionRuleService {

    @Autowired
    private QuestionRuleRepository questionRuleRepository;

    @Override
    public QuestionRule addQuestionRule(QuestionRule questionRule) {
        return questionRuleRepository.save(questionRule);
    }

    @Override
    public QuestionRule updateQuestionRule(Long id, QuestionRule questionRule) {
        QuestionRule existing = questionRuleRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("QuestionRule not found!"));
        existing.setName(questionRule.getName());
        existing.setFillQuestionCount(questionRule.getFillQuestionCount());
        existing.setMultiQuestionCount(questionRule.getMultiQuestionCount());
        existing.setJudgeQuestionCount(questionRule.getJudgeQuestionCount());
        existing.setSubject(questionRule.getSubject());
        return questionRuleRepository.save(existing);
    }

    @Override
    public void deleteQuestionRule(Long id) {
        questionRuleRepository.deleteById(id);
    }

    @Override
    public QuestionRule getQuestionRuleById(Long id) {
        return questionRuleRepository.findById(id).orElse(null);
    }

    @Override
    public Page<QuestionRule> getAllQuestionRules(Pageable pageable) {
        return questionRuleRepository.findAll(pageable);
    }
}